// 环境检测工具
export const getEnvironmentInfo = () => {
  return {
    // 基础环境信息
    isProduction: import.meta.env.PROD,
    isDevelopment: import.meta.env.DEV,
    mode: import.meta.env.MODE,
    baseUrl: import.meta.env.BASE_URL,
    
    // 浏览器信息
    userAgent: navigator.userAgent,
    language: navigator.language,
    platform: navigator.platform,
    
    // 性能信息
    connection: navigator.connection ? {
      effectiveType: navigator.connection.effectiveType,
      downlink: navigator.connection.downlink,
      rtt: navigator.connection.rtt
    } : null,
    
    // 存储信息
    localStorage: typeof localStorage !== 'undefined',
    sessionStorage: typeof sessionStorage !== 'undefined',
    
    // 时间信息
    timestamp: Date.now(),
    timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
  }
}

// 检查是否在Nginx环境中
export const isNginxEnvironment = () => {
  const userAgent = navigator.userAgent.toLowerCase()
  const isServer = window.location.hostname !== 'localhost' && 
                   window.location.hostname !== '127.0.0.1'
  
  return {
    isServer,
    isLocalhost: !isServer,
    hasHashRouter: window.location.hash !== '',
    hasHistoryRouter: window.location.pathname !== '/'
  }
}

// 性能监控
export const monitorPerformance = () => {
  const startTime = performance.now()
  
  return {
    getLoadTime: () => performance.now() - startTime,
    getNavigationTiming: () => {
      if (performance.getEntriesByType) {
        const navigation = performance.getEntriesByType('navigation')[0]
        return navigation ? {
          domContentLoaded: navigation.domContentLoadedEventEnd - navigation.domContentLoadedEventStart,
          loadComplete: navigation.loadEventEnd - navigation.loadEventStart,
          total: navigation.loadEventEnd - navigation.fetchStart
        } : null
      }
      return null
    }
  }
}

// 错误监控
export const setupErrorMonitoring = () => {
  const errors = []
  
  window.addEventListener('error', (event) => {
    errors.push({
      type: 'error',
      message: event.message,
      filename: event.filename,
      lineno: event.lineno,
      colno: event.colno,
      timestamp: Date.now()
    })
    console.error('JavaScript Error:', event)
  })
  
  window.addEventListener('unhandledrejection', (event) => {
    errors.push({
      type: 'unhandledrejection',
      reason: event.reason,
      timestamp: Date.now()
    })
    console.error('Unhandled Promise Rejection:', event.reason)
  })
  
  return {
    getErrors: () => errors,
    clearErrors: () => errors.length = 0
  }
}

// 资源加载监控
export const monitorResourceLoading = () => {
  const resources = []
  
  if (performance.getEntriesByType) {
    performance.getEntriesByType('resource').forEach(resource => {
      resources.push({
        name: resource.name,
        type: resource.initiatorType,
        duration: resource.duration,
        size: resource.transferSize,
        startTime: resource.startTime
      })
    })
  }
  
  return resources
} 